const fs = require('fs');
const pool = require('../models/db');
const bcrypt = require('bcrypt');
const saltRounds = 10;  // 加密强度,和accountsService下的一致

async function insertData() {
    const accounts = JSON.parse(fs.readFileSync('./data/accounts.txt', 'utf-8'));
    const goods = JSON.parse(fs.readFileSync('./data/goods.txt', 'utf-8'));
    const purchases = JSON.parse(fs.readFileSync('./data/purchases.txt', 'utf-8'));
    const sales = JSON.parse(fs.readFileSync('./data/sales.txt', 'utf-8'));
    const stock = JSON.parse(fs.readFileSync('./data/stock.txt', 'utf-8'));
    const schemaSql = fs.readFileSync('./data/schema.sql', 'utf-8');

    if(!schemaSql){
        console.log("schemaSql is null")
    }
    try {
        await pool.query('BEGIN');

        // 执行 schema.sql ,创建表格
        await pool.query(schemaSql);

        for (const account of accounts) {
            const hashedPassword = await bcrypt.hash(account.account_password, saltRounds);
            await pool.query(
                'INSERT INTO accounts (account_nickname, account_password, permission_level, full_name, gender, contact_info, remarks) VALUES ($1, $2, $3, $4, $5, $6, $7)',
                [account.account_nickname, hashedPassword, account.permission_level, account.full_name, account.gender, account.contact_info, account.remarks]
            );
        }

        for (const good of goods) {
            await pool.query(
                'INSERT INTO goods (goods_name, goods_type, remarks) VALUES ($1, $2, $3)',
                [good.goods_name, good.goods_type, good.remarks]
            );
        }

        for (const purchase of purchases) {
            await pool.query(
                'INSERT INTO purchase (goods_id, purchase_price, purchase_quantity, purchase_time, remarks) VALUES ($1, $2, $3, $4, $5)',
                [purchase.goods_id, purchase.purchase_price, purchase.purchase_quantity, purchase.purchase_time, purchase.remarks]
            );
        }

        for (const sale of sales) {
            await pool.query(
                'INSERT INTO sales (goods_id, sales_price, sales_quantity, sales_time, remarks) VALUES ($1, $2, $3, $4, $5)',
                [sale.goods_id, sale.sales_price, sale.sales_quantity, sale.sales_time, sale.remarks]
            );
        }

        for (const stockItem of stock) {
            await pool.query(
                'INSERT INTO stock (goods_id, stock_quantity, warning_threshold, stock_status, remarks) VALUES ($1, $2, $3, $4, $5)',
                [stockItem.goods_id, stockItem.stock_quantity, stockItem.warning_threshold, stockItem.stock_status, stockItem.remarks]
            );
        }

        await pool.query('COMMIT');
    } catch (error) {
        await pool.query('ROLLBACK');
        console.error('插入数据失败:', error);
    } finally {
        pool.end();
    }
}

async function clearDatabase() {
    try {
        await pool.query('BEGIN');
        await pool.query('TRUNCATE TABLE accounts, goods, purchase, sales, stock RESTART IDENTITY CASCADE');
        await pool.query('COMMIT');
    } catch (error) {
        await pool.query('ROLLBACK');
        console.error('清空数据库失败:', error);
    } finally {
        pool.end();
    }
}

module.exports = { insertData, clearDatabase };

// test
insertData();
// clearDatabase();
